import { Node, v3, Vec3 } from "cc";
import { SmartNodePool } from "../../scripts/libs/utils/SmartNodePool";
import { rm } from "../../scripts/managers/ResourceManager";
import { Caidai } from "../model3d/_caidai/Caidai";
import { GenCircleRandomPos } from "../../scripts/libs/utils/math";

const _v30 = v3();

export class CaidaiHelper {

    static PrefabPath_Caidai_0 = 'ab_lobby/model3d/_caidai/Caidai-0';
    static PrefabPath_Caidai_1 = 'ab_lobby/model3d/_caidai/Caidai-1';
    static PrefabPath_Caidai_2 = 'ab_lobby/model3d/_caidai/Caidai-2';

    // caidai
    static caidaiPool_0 = new SmartNodePool();
    static caidaiPool_1 = new SmartNodePool();
    static caidaiPool_2 = new SmartNodePool();
    static PrepareCaidai(count: number) {
        this.caidaiPool_0.setPrefab(rm.prefabs[this.PrefabPath_Caidai_0]);
        this.caidaiPool_0.prepare(count);
        this.caidaiPool_1.setPrefab(rm.prefabs[this.PrefabPath_Caidai_1]);
        this.caidaiPool_1.prepare(count);
        this.caidaiPool_2.setPrefab(rm.prefabs[this.PrefabPath_Caidai_2]);
        this.caidaiPool_2.prepare(count);
    }
    static GetCaidai(id: number) {
        if (id == 0) {
            return this.caidaiPool_0.getNode();
        } else if (id == 1) {
            return this.caidaiPool_1.getNode();
        } else if (id == 2) {
            return this.caidaiPool_2.getNode();
        }
    }
    static PutCaidai(node: Node) {
        if (node.name == 'Caidai-0') {
            this.caidaiPool_0.putNode(node);
        } else if (node.name == 'Caidai-1') {
            this.caidaiPool_1.putNode(node);
        } else if (node.name == 'Caidai-2') {
            this.caidaiPool_2.putNode(node);
        }
    }

    static BurstCaidai(parent: Node, count: number, startWorldPos: Vec3, targetPosition: Vec3) {
        for (let i = 0; i < count; i++) {
            let id = Math.floor(Math.random() * 3);
            let caidaiNode = this.GetCaidai(id);
            caidaiNode.setParent(parent);
            caidaiNode.setWorldPosition(startWorldPos);
            let caidai = caidaiNode.getComponent(Caidai);
            caidai.endCallback = () => {
                this.PutCaidai(caidaiNode);
            };
            caidai.init();

            _v30.set(targetPosition);
            let { x, y } = GenCircleRandomPos(550);
            _v30.x += x;
            _v30.y += y;

            _v30.subtract(startWorldPos);
            let distance = _v30.length();
            caidai.burst(_v30, distance);
        }
    }
}

globalThis.CaidaiHelper = CaidaiHelper;